<?php

namespace App\Http\Controllers;

use App\Http\Requests\ClassesRequest;
use App\Http\Resources\ClassesCollection;
use App\Http\Resources\ClassesResource;
use App\Models\Classes;
use App\Services\ClassesService;

class ClassesController extends Controller
{
    public function index(ClassesRequest $request)
    {
        $params = $request->validated();

        $builder = ClassesService::getInstance()->search($params);
        return ClassesCollection::make($builder);
    }

    public function show(Classes $classes)
    {
        $builder = $classes->with('student:id,class_id,name')->first();

        return ClassesResource::make($builder);
    }

    public function create(ClassesRequest $request, Classes $classes)
    {
        $params = $request->validated();

        $status = $classes->fill($params)->save();
        return response()->json($status);
    }

    public function update(ClassesRequest $request, $id)
    {
        $params = $request->validated();
        $classes = Classes::find($id);
        $status = $classes->fill($params)->save();
        return response()->json($status);
    }

    public function destroy($id)
    {
        $classes = Classes::find($id);
        $status = $classes->delete();
        return response()->json($status);
    }
}
